package com.dms.modules.merchant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dms.common.api.ApiResponse;
import com.dms.common.security.SecurityUtils;
import com.dms.modules.merchant.dto.MerchantCommentBatchReplyDTO;
import com.dms.modules.merchant.dto.MerchantCommentTemplateDTO;
import com.dms.modules.merchant.service.MerchantCommentService;
import com.dms.modules.product.vo.CommentStatisticsVO;
import com.dms.modules.merchant.vo.MerchantCommentTrendVO;
import com.dms.modules.product.vo.CommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 商家评论控制器
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/merchant/comments")
@Api(tags = "商家评论管理接口")
@PreAuthorize("hasRole('MERCHANT')")
public class MerchantCommentController {

    @Autowired
    private MerchantCommentService merchantCommentService;

    @GetMapping("/all")
    @ApiOperation("获取商家所有商品的评论列表")
    public ApiResponse<IPage<CommentVO>> getAllComments(
            @RequestParam(defaultValue = "1") @Min(value = 1, message = "页码不能小于1") Integer pageNum,
            @RequestParam(defaultValue = "10") @Min(value = 1, message = "每页条数不能小于1") Integer pageSize,
            @ApiParam("评分") @RequestParam(required = false) Integer rating,
            @ApiParam("是否有图") @RequestParam(required = false) Boolean hasImage,
            @ApiParam("排序字段") @RequestParam(required = false, defaultValue = "createTime") String sortField,
            @ApiParam("排序方式") @RequestParam(required = false, defaultValue = "desc") String sortOrder) {
        Long merchantId = SecurityUtils.getCurrentUserId();
        log.info("商家[{}]获取所有商品评论列表, pageNum={}, pageSize={}, rating={}, hasImage={}, sortField={}, sortOrder={}", 
            merchantId, pageNum, pageSize, rating, hasImage, sortField, sortOrder);
        Page<CommentVO> page = new Page<>(pageNum, pageSize);
        return ApiResponse.success(merchantCommentService.getMerchantComments(merchantId, page, rating, hasImage, sortField, sortOrder));
    }

    @GetMapping("/product/{productId}")
    @ApiOperation("获取商品评论列表")
    public ApiResponse<IPage<CommentVO>> getProductComments(
            @PathVariable @NotNull(message = "商品ID不能为空") Long productId,
            @RequestParam(defaultValue = "1") @Min(value = 1, message = "页码不能小于1") Integer pageNum,
            @RequestParam(defaultValue = "10") @Min(value = 1, message = "每页条数不能小于1") Integer pageSize) {
        Page<CommentVO> page = new Page<>(pageNum, pageSize);
        return ApiResponse.success(merchantCommentService.getProductComments(productId, page));
    }

    @PostMapping("/{id}/reply")
    @ApiOperation("回复评论")
    public ApiResponse<Boolean> replyComment(
            @ApiParam("评论ID") @PathVariable @NotNull(message = "评论ID不能为空") Long id,
            @ApiParam("回复内容") @RequestParam @NotBlank(message = "回复内容不能为空") String reply) {
        Long userId = SecurityUtils.getCurrentUserId();
        return ApiResponse.success(merchantCommentService.replyComment(id, reply, userId));
    }

    @PostMapping("/batch-reply")
    @ApiOperation("批量回复评论")
    public ApiResponse<Boolean> batchReplyComments(@RequestBody @Validated MerchantCommentBatchReplyDTO dto) {
        return ApiResponse.success(merchantCommentService.batchReplyComments(dto));
    }

    @GetMapping("/statistics/{productId}")
    @ApiOperation("获取评论统计")
    public ApiResponse<CommentStatisticsVO> getCommentStatistics(
            @PathVariable @NotNull(message = "商品ID不能为空") Long productId) {
        return ApiResponse.success(merchantCommentService.getCommentStatistics(productId));
    }

    @GetMapping("/trend/{productId}")
    @ApiOperation("获取评论趋势")
    public ApiResponse<List<MerchantCommentTrendVO>> getCommentTrend(
            @PathVariable @NotNull(message = "商品ID不能为空") Long productId,
            @RequestParam(defaultValue = "7") @Min(value = 1, message = "天数不能小于1") Integer days) {
        return ApiResponse.success(merchantCommentService.getCommentTrend(productId, days));
    }

    @GetMapping("/export/{productId}")
    @ApiOperation("导出评论")
    public void exportComments(
            @PathVariable @NotNull(message = "商品ID不能为空") Long productId,
            HttpServletResponse response) {
        merchantCommentService.exportComments(productId, response);
    }

    @PostMapping("/template")
    @ApiOperation("创建回复模板")
    public ApiResponse<Long> createReplyTemplate(@RequestBody @Validated MerchantCommentTemplateDTO dto) {
        Long userId = SecurityUtils.getCurrentUserId();
        return ApiResponse.success(merchantCommentService.createReplyTemplate(dto, userId));
    }

    @PutMapping("/template/{id}")
    @ApiOperation("更新回复模板")
    public ApiResponse<Boolean> updateReplyTemplate(
            @PathVariable @NotNull(message = "模板ID不能为空") Long id,
            @RequestBody @Validated MerchantCommentTemplateDTO dto) {
        Long userId = SecurityUtils.getCurrentUserId();
        return ApiResponse.success(merchantCommentService.updateReplyTemplate(id, dto, userId));
    }

    @DeleteMapping("/template/{id}")
    @ApiOperation("删除回复模板")
    public ApiResponse<Boolean> deleteReplyTemplate(
            @PathVariable @NotNull(message = "模板ID不能为空") Long id) {
        return ApiResponse.success(merchantCommentService.deleteReplyTemplate(id));
    }

    @GetMapping("/templates")
    @ApiOperation("获取回复模板列表")
    public ApiResponse<List<MerchantCommentTemplateDTO>> getReplyTemplates() {
        return ApiResponse.success(merchantCommentService.getReplyTemplates());
    }
} 